package com.github.iot.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.iot.R;
import com.github.iot.entity.Door;
import com.github.iot.entity.DoorType;
import com.github.iot.utils.FastJsonUtils;

import java.util.List;

/**
 * 柜门适配器
 *
 * @Author: 梁声洪
 * @Date: 2019/12/24 22:47
 * @Version: v1.0.0
 */
public class DoorTypeAdapter extends RecyclerView.Adapter<DoorTypeAdapter.DoorViewHolder> {

    private Context context;
    private List<DoorType> list;
    private View inflater;

    public DoorTypeAdapter(Context context, List<DoorType> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public DoorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        inflater = LayoutInflater.from(context).inflate(R.layout.door_type_item, parent, false);
        DoorViewHolder viewHolder = new DoorViewHolder(inflater);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(DoorViewHolder holder, int position) {
        DoorType type = list.get(position);
        holder.mTypeName.setText(type.getTypeName());

        List<Door> doorTypeList = type.getDoorList();
        Log.i("LSHENGH", "DoorTypeAdapter: " + FastJsonUtils.obj2JsonStr(doorTypeList));

        if (doorTypeList != null && !doorTypeList.isEmpty()) {
            DoorAdapter doorAdapter = new DoorAdapter(context, doorTypeList);
            holder.mDoor.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
            holder.mDoor.setAdapter(doorAdapter);

            // 可选：添加分割线
            if (holder.mDoor.getItemDecorationCount() == 0) {
                DividerItemDecoration divider = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL);
                holder.mDoor.addItemDecoration(divider);
            }
        } else {
            // 当没有数据时，可以设置空适配器或隐藏 RecyclerView
            holder.mDoor.setAdapter(null);
            holder.mDoor.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class DoorViewHolder extends RecyclerView.ViewHolder {

        TextView mTypeName;
        RecyclerView mDoor;

        public DoorViewHolder(View itemView) {
            super(itemView);
            mTypeName = itemView.findViewById(R.id.tv_type_name);
            mDoor = itemView.findViewById(R.id.rv_door);
        }
    }

}